Gmail & SMTP IPC Handlers
This document provides comprehensive documentation for the Gmail and SMTP IPC handlers in the Electron-based bulk messaging application. It covers:
Gmail OAuth2 authentication flow initiation, authorization URL generation, and callback handling
Gmail token retrieval and validation
Email composition, attachment handling, and batch processing via Gmail API
SMTP email sending with connection parameters, authentication methods, and queue-like batch processing
Parameter validation schemas, error handling patterns, and retry mechanisms
Examples of email template processing, HTML content rendering, and progress tracking
Security considerations for credential storage and transmission
The handlers are exposed via Electron’s IPC mechanism and consumed by React components in the renderer process.
The relevant parts of the project structure for this documentation are:
Electron main process handlers: gmail-handler.js, smtp-handler.js
Electron main process registration: main.js
Preload bridge exposing IPC APIs: preload.js
Frontend components using the handlers: GmailForm.jsx, SMTPForm.jsx, BulkMailer.jsx
Application documentation and configuration: README.md, electron/package.json
Diagram sources
Section sources
Gmail IPC handlers:
gmail-auth: Initiates OAuth2 flow, opens authorization window, handles redirect callback, exchanges code for tokens, stores credentials
gmail-token: Checks local token availability
send-email: Sends emails via Gmail API with progress events and rate limiting
SMTP IPC handler:
smtp-send: Validates SMTP config, creates transport, verifies connectivity, sends emails with progress events and rate limiting
Frontend integration:
Preload exposes invokeable APIs for renderer
GmailForm and SMTPForm orchestrate user actions and display progress
BulkMailer coordinates form validation and handler invocation
Key runtime dependencies:
googleapis for Gmail API
nodemailer for SMTP
electron-store for secure local storage
Section sources
The IPC architecture follows Electron’s model:
Renderer (React) invokes preload-exposed APIs
Preload forwards IPC invocations to main process handlers
Main process handlers perform external operations (OAuth, API calls, SMTP)
Handlers emit progress events back to renderer via IPC channels
Diagram sources
Gmail IPC Handlers#
Handler: gmail-auth#
Purpose: Initiate OAuth2 flow for Gmail
Steps:
Validate environment variables for client ID and secret
Create OAuth2 client with configured redirect URI
Generate authorization URL with offline access and consent prompt
Open BrowserWindow to display authorization page
Listen for redirect to capture authorization code
Exchange code for tokens and store credentials securely
Resolve promise with success/failure result
Timeout: 5 minutes for OAuth completion
Error handling: Catches missing env vars, token exchange errors, and window closure
Resolve timeout"] GotCode --> |Yes| TokenExchange["getToken(code)"] TokenExchange --> StoreCreds["store.set('gmail_token')"] StoreCreds --> ResolveSuccess["Resolve success"]
Diagram sources
Section sources
Handler: gmail-token#
Purpose: Check if a stored token exists
Behavior: Reads token from electron-store and returns presence flag
Use case: UI enables/disables Gmail actions based on token presence
Section sources
Handler: send-email#
Purpose: Send bulk emails via Gmail API
Input schema:
recipients: array of email addresses
subject: string
message: HTML string
delay: number (optional, milliseconds)
Processing:
Validates token presence
Sets up OAuth2 client with stored token
Iterates recipients, emits progress events
Constructs MIME message with HTML content-type
Sends via gmail.users.messages.send
Applies rate limiting delay between emails
Output schema:
success: boolean
results: array of { recipient, status, error? }
Diagram sources
Section sources
SMTP IPC Handler#
Handler: smtp-send#
Purpose: Send bulk emails via SMTP
Input schema:
smtpConfig: { host, port, secure, user, pass }
recipients: array of email addresses
subject: string
message: HTML string
delay: number (optional)
saveCredentials: boolean (optional)
Processing:
Validates smtpConfig fields
Optionally saves sanitized config to electron-store
Creates nodemailer transport with TLS settings
Verifies connection via transporter.verify()
Iterates recipients, emits progress events
Builds mailOptions with HTML and plain text
Sends via transporter.sendMail()
Applies rate limiting delay between emails
Output schema:
success: boolean
results: array of { recipient, status, error? }
Diagram sources
Section sources
Frontend Integration and Progress Tracking#
Preload exposes:
authenticateGmail, getGmailToken, sendEmail
sendSMTPEmail
onProgress for email-progress events
GmailForm and SMTPForm:
Validate forms and recipients
Trigger handler invocations
Render activity log with status and errors
Progress events:
Current/total counters
Per-recipient status (sending/sent/failed)
Error details for failures
Section sources
External libraries and their roles:
googleapis: Gmail API client for OAuth2 and sending messages
nodemailer: SMTP transport creation and email sending
electron-store: Secure local storage for tokens/configs
dotenv: Environment variable loading for OAuth credentials
Diagram sources
Section sources
Rate limiting: Both handlers apply configurable delays between emails to avoid throttling and reduce spam risk.
Batch processing: Iterative loop with per-recipient progress updates ensures visibility and graceful failure handling.
Connection verification: SMTP handler verifies transport before sending to minimize failures mid-batch.
Memory footprint: Gmail handler constructs base64-encoded MIME messages; consider message size limits and HTML complexity.
[No sources needed since this section provides general guidance]
Common issues and resolutions:
Gmail OAuth timeout or window closed:
Increase timeout window or re-initiate authentication
Ensure redirect URI matches configured value
Missing environment variables:
Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in .env
Gmail API errors:
Verify OAuth scopes and consent screen configuration
Confirm token storage and refresh behavior
SMTP connection failures:
Validate host/port/security settings
Use correct authentication credentials
Check firewall and TLS settings
Progress tracking not updating:
Ensure onProgress listeners are attached in renderer
Verify event channel names match
Section sources
The Gmail and SMTP IPC handlers provide robust, secure, and user-friendly mechanisms for bulk email sending. They integrate seamlessly with Electron’s IPC model, offer comprehensive error handling and progress tracking, and adhere to security best practices for credential storage and transmission. The frontend components deliver a polished user experience with real-time feedback and validation.
[No sources needed since this section summarizes without analyzing specific files]
Parameter Validation Schemas#
Gmail send-email input:
recipients: array
subject: string
message: string (HTML)
delay?: number (default 1000 ms)
SMTP send-email input:
smtpConfig: { host, port, secure, user, pass }
recipients: array
subject: string
message: string (HTML)
delay?: number (default 1000 ms)
saveCredentials?: boolean
Section sources
Security Considerations#
OAuth2 credentials:
Stored in environment variables; loaded via dotenv
Tokens stored in electron-store; avoid logging sensitive data
SMTP credentials:
Passwords are not saved to disk; only sanitized config is persisted
Transport security:
TLS settings configured; self-signed certificate handling included
UI isolation:
Context isolation enabled; preload bridge restricts exposed APIs
Section sources
Example Workflows#
Gmail OAuth2 flow:
User clicks “Authenticate Gmail”
App opens authorization window
User grants consent and receives code
App exchanges code for tokens and stores them
Bulk email sending (Gmail):
User enters recipients, subject, and HTML message
App invokes send-email with delay
App displays per-recipient progress and final results
Bulk email sending (SMTP):
User configures SMTP settings and imports recipients
App validates config and verifies transport
App sends emails with HTML and plain text variants
Section sources